﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Aspect;
using Pixysoft.Framework.XSchEmA.Hinter;
using Pixysoft.Framework.XSchEmA.Entity;

namespace Pixysoft.Framework.XSchEmA.Modifier
{
    partial class ColumnModifier : IColumnModifier
    {
        ISchemaAspect aspect;

        public ColumnModifier(ISchemaAspect aspect)
        {
            this.aspect = aspect;
        }


        /// <summary>
        /// 修改被引用的外键
        /// Pre Condition:表名经过验证
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="datatype">The datatype.</param>
        /// <param name="database">The database.</param>
        /// <param name="table">The table.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        public bool ChangeColumnName(DatabaseSchema database, TableSchema table, ColumnSchema column, string columnName)
        {
            column.ColumnName = columnName.ToUpper().Trim();
            database.RefreshUniques();
            database.RefreshPrimaryKeys();
            database.RefreshForeignKeys();
            return true;
        }

        /// <summary>
        /// Changes the name of the pk.
        /// </summary>
        /// <param name="pkName">Name of the pk.</param>
        /// <param name="database">The database.</param>
        /// <param name="table">The table.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        private bool ChangeUniqueName(DatabaseSchema database, TableSchema table, List<ColumnSchema> columns, string constraintName)
        {
            if (columns == null || columns.Count == 0)
                throw new Exception();

            UniqueSchema unique = database.GetUnique(columns);
            unique.ConsName = constraintName;
            database.RefreshForeignKeys();
            return true;
        }

        /// <summary>
        /// Changes the name of the pk.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="table">The table.</param>
        /// <param name="pkName">Name of the pk.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        private bool ChangePkName(DatabaseSchema database, TableSchema table, string pkName)
        {

            PrimaryKeySchema pk = database.GetPrimaryKey(table);

            pk.ConsName = pkName;

            database.RefreshForeignKeys();

            return true;
        }

        /// <summary>
        /// Changes the name of the fk.
        /// </summary>
        /// <param name="fkName">Name of the fk.</param>
        /// <param name="database">The database.</param>
        /// <param name="table">The table.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        private bool ChangeFkName(DatabaseSchema database, TableSchema table, List<ColumnSchema> columns, string fkName)
        {
            if (columns == null || columns.Count == 0)
                throw new Exception();

            ForeignKeySchema fk = database.GetForeignKey(columns);

            fk.ConsName = fkName;

            return true;
        }

        /// <summary>
        /// Changes the memo.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public bool ChangeComment(ColumnSchema column, string comment)
        {
            column.Comment = comment;

            return true;
        }

        /// <summary>
        /// Changes the default value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="datatype">The datatype.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        public bool ChangeDefaultValue(ColumnSchema column, string value)
        {
            column.DefaultValue = aspect.ValidateDefaultValue(value, column.DataType);

            return true;
        }
    }
}
